package com.kun.video.util;

import cn.hutool.core.util.StrUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * TODO
 *
 * @author gzc
 * @since 2025/3/27
 **/
public class FFmpegParser {
    private final static Pattern DURATION_PATTERN = Pattern.compile("Duration: (\\d{2}:\\d{2}:\\d{2}\\.\\d{2})");
    private final static Pattern BITRATE_PATTERN = Pattern.compile("bitrate: (\\d+) kb/s");
    private final static Pattern DIMENSIONS_PATTERN = Pattern.compile("(\\d+)x(\\d+) \\[SAR");
    private final static Pattern FPS_PATTERN = Pattern.compile("(\\d+) fps");
    private final static Pattern VIDEO_CODEC_PATTERN = Pattern.compile("Video: ([^,]+)");
    private final static Pattern AUDIO_CODEC_PATTERN = Pattern.compile("Audio: ([^,]+)");

    private final String ffmpegResult;

    public FFmpegParser(String ffmpegResult) {
        this.ffmpegResult = ffmpegResult;
    }

    public String getDuration() {
        Matcher matcher = DURATION_PATTERN.matcher(ffmpegResult);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }

    public String getBitrate() {
        Matcher matcher = BITRATE_PATTERN.matcher(ffmpegResult);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }

    public double[] getDimensions() {
        Matcher matcher = DIMENSIONS_PATTERN.matcher(ffmpegResult);
        if (matcher.find()) {
            return new double[]{Double.parseDouble(matcher.group(1)), Double.parseDouble(matcher.group(2))};
        }
        return new double[0];
    }

    public double getWidth() {
        Matcher matcher = DIMENSIONS_PATTERN.matcher(ffmpegResult);
        if (matcher.find()) {
           return   Double.parseDouble(matcher.group(1));
        }
        Pattern pattern = Pattern.compile(
                "Video: ([^,]+).*?(\\d{3,}x\\d{3,}).*?(\\d+ kb/s).*?(\\d+) fps"
        );
        Matcher matcher1 = pattern.matcher(ffmpegResult);
        if (matcher1.find()) {
            String[] split = matcher1.group(2).split("x");
            return Double.parseDouble(split[0]);
        }
        return 0;
    }

    public double getHeight() {
        Matcher matcher = DIMENSIONS_PATTERN.matcher(ffmpegResult);
        if (matcher.find()) {
            return Double.parseDouble(matcher.group(2));
        }
        Pattern pattern = Pattern.compile(
                "Video: ([^,]+).*?(\\d{3,}x\\d{3,}).*?(\\d+ kb/s).*?(\\d+) fps"
        );
        Matcher matcher1 = pattern.matcher(ffmpegResult);
        if (matcher1.find()) {
            String[] split = matcher1.group(2).split("x");
            return Double.parseDouble(split[1]);
        }
        return 0;
    }

    public String getResolution() {
        Matcher matcher = DIMENSIONS_PATTERN.matcher(ffmpegResult);
        if (matcher.find()) {
            return getWidth() + "x" + getHeight();
        }
        return "";
    }

    public long getDurationMillis() {
        String duration = getDuration();
        if (StrUtil.isBlank(duration)) {
            return 0;
        }
        String[] parts = duration.split(":");
        int hours = Integer.parseInt(parts[0]);
        int minutes = Integer.parseInt(parts[1]);
        String[] secondsParts = parts[2].split("\\.");
        int seconds = Integer.parseInt(secondsParts[0]);
        // 处理百分秒（允许空值，如 "10."）
        int hundredths = secondsParts.length > 1 && !secondsParts[1].isEmpty() ?
                Integer.parseInt(secondsParts[1]) : 0;

        return hours * 3600_000L
                + minutes * 60_000L
                + seconds * 1000L
                + hundredths * 10;
    }

    public String getFps() {
        Matcher matcher = FPS_PATTERN.matcher(ffmpegResult);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }

    public String getVideoCodec() {
        Matcher matcher = VIDEO_CODEC_PATTERN.matcher(ffmpegResult);
        if (matcher.find()) {
            return matcher.group(1).trim();
        }
        return "";
    }

    public String getAudioCodec() {
        Matcher matcher = AUDIO_CODEC_PATTERN.matcher(ffmpegResult);
        if (matcher.find()) {
            return matcher.group(1).trim();
        }
        return "";
    }
}
